package com.kyh.util;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class QrcodeUtil {
	
	
	/*public static void decodeQrcode() {
		
		String urlImg = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555746454037&di=b190ec2b5f11a441102ee3c93459e92a&imgtype=0&src=http%3A%2F%2Fimg1.gtimg.com%2Fgamezone%2Fpics%2Fhv1%2F8%2F38%2F1602%2F104179748.jpg";
		
		try {
			URL url = new URL(urlImg);
			BufferedImage bufferedImage = ImageIO.read(url);
			QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	
	public static String decodeByUrl(String urlImg) {
        String content = null;
        BufferedImage image;
        try {
        	URL url = new URL(urlImg);
            image = ImageIO.read(url);
            
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码
            content = result.getText();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (NotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return content;
	}
}
